package ua.kharkov.knure.dereza.summarytask4.command;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import ua.kharkov.knure.dereza.summarytask4.core.Constants;
import ua.kharkov.knure.dereza.summarytask4.db.bean.ClientBean;
import ua.kharkov.knure.dereza.summarytask4.db.dao.CardDAO;
import ua.kharkov.knure.dereza.summarytask4.db.dao.DAOFactory;
import ua.kharkov.knure.dereza.summarytask4.db.dao.StatusDAO;
import ua.kharkov.knure.dereza.summarytask4.db.dao.mysql.MysqlDAOFactory;
import ua.kharkov.knure.dereza.summarytask4.db.dto.CardDTO;
import ua.kharkov.knure.dereza.summarytask4.db.dto.StatusDTO;

/**
 * Lock card command
 * 
 * @author dereza
 * 
 */
public class LockCardCommand extends Command {

	private static final long serialVersionUID = -2083902318868769795L;

	private static final Logger log = Logger.getLogger(LockCardCommand.class);

	@Override
	public String execute(HttpServletRequest req, HttpServletResponse res) {
		log.debug("Command started");

		// gets client's session
		HttpSession session = req.getSession(false);

		// gets ClientBean from session
		ClientBean clientBean = (ClientBean) session.getAttribute("clientbean");
		log.trace("Got clientbean  from session --> " + clientBean);

		// if clientBean == null forward to login page
		if (clientBean == null) {
			String forward = Constants.COMMAND_LOGOUT;
			return forward;
		}

		// gets card id from request
		int cardId = Integer.parseInt(req.getParameter("id"));
		log.trace("got id card --> " + cardId);

		// creates the required DAO generator
		MysqlDAOFactory mysqlFactory = (MysqlDAOFactory) DAOFactory
				.getDAOFactory(DAOFactory.MYSQL);

		// / creates DAO for card
		CardDAO cardDAO = mysqlFactory.getCardDAO();
		CardDTO cardDTO = cardDAO.findClientCardById(cardId);

		// error handler
		String errorMessage = null;
		String forward = Constants.PAGE_ERROR_PAGE;
		if (cardDTO == null) {
			errorMessage = "Internal error. Cannot find card";
			req.setAttribute("errorMessage", errorMessage);
			log.error("errorMessage --> " + errorMessage);
			return forward;
		}
		log.trace("Found card in db --> " + cardDTO);

		// gets status dao
		StatusDAO statusDAO = mysqlFactory.getStatusDAO();
		StatusDTO statusDTO = statusDAO
				.findStatusByName(Constants.STATUS_LOCKED);
		int statusId = statusDTO.getId();

		// check ability operation
		if (cardDTO.getStatus_id() == statusId) {
			errorMessage = "Card is already locked";
			req.setAttribute("errorMessage", errorMessage);
			log.error("errorMessage --> " + errorMessage);
			return forward;
		}

		// execute locking
		cardDAO.updateStatus(cardId, statusId);
		log.info("Card was locked --> " + cardDTO.getId() + ", with code = "
				+ cardDTO.getCode());
		forward = Constants.COMMAND_CLIENT_LIST_CARDS;
		log.debug("Command finished");
		return forward;
	}

	@Override
	public String toString() {
		return "LockCardCommand";
	}

}
